﻿using QG.FrontDesk.Core;
using QG.FrontDesk.Core.Encrypt;
using QG.FrontDesk.DAL.IRepository;
using QG.FrontDesk.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QG.FrontDesk.DAL.Repository
{
    public class UserInfoRepository : IUserInfoRepository
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public UserInfo Getlogin(UserInfo userInfo)
        {
            string sql = "SELECT * FROM `userinfo` WHERE `email` = @email AND `userPwd` = @userPwd";
            return DapperDBHelper<UserInfo>.QueryFirstOrDefault(sql, userInfo);
        }

        /// <summary>
        /// 新增QQ用户
        /// </summary>
        /// <returns></returns>
        public bool AddQQUserInfo(UserInfo userInfo) {
            string sql= "INSERT INTO userinfo(`userName`,`sex`,`QQ`,`STATUS`,`touXiang`) VALUE (@userName,@sex,@QQ,1,@touXiang)";
            return DapperDBHelper<UserInfo>.Execute(sql,userInfo)>0;
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public bool AddUserInfo(UserInfo userInfo)
        {
            string sql = "INSERT INTO `userinfo`(`userName`,`userPwd`,`realName`,`sex`,`email`,`education`,`workExperien`,`phone`,`QQ`,`sinaMicroblog`,`STATUS`,`touXiang`)VALUES(@userName, @userPwd, @realName, @sex, @email, @education, @workExperien, @phone, @QQ, @sinaMicroblog, @STATUS, @touXiang); SELECT @@identity";
            int userId = DapperDBHelper<UserInfo>.Query(sql, userInfo);
            if (userId> 0) {
                sql = "INSERT INTO businessexperien VALUE(NULL,'','',now(),DEFAULT); SELECT @@identity";
                int experId = DapperDBHelper<Businessexperien>.Query(sql,null);
                sql = "INSERT INTO worksshow VALUE(NULL,NULL,NULL);  SELECT @@identity";
                int showId = DapperDBHelper<WorksShow>.Query(sql,null);
                sql = "INSERT INTO projectexperien VALUE(NULL,'','',now(),DEFAULT,NULL); SELECT @@identity";
                int prodId = DapperDBHelper<ProjectExperien>.Query(sql,null);
                sql = "INSERT INTO educational VALUE(NULL,'',NULL,NULL,NOW(),DEFAULT);  SELECT @@identity";
                int educId = DapperDBHelper<Educational>.Query(sql,null);
                sql = "INSERT INTO expecttowork VALUE(NULL,1,NULL,NULL,NULL); SELECT @@identity";
                int workId = DapperDBHelper<ExpecttoWork>.Query(sql,null) ;
                sql = "INSERT INTO resumeinfo VALUE(NULL,@userId,@workId,@experId,@proId,@educId,NULL,@showId,0,NULL,NULL)";
                return DapperDBHelper<ResumeInfo>.Execute(sql,new ResumeInfo() {
                    UserId= userId,
                    ExperId=experId,
                    ShowId=showId,
                    ProId=prodId,
                    EducId=educId,
                    WorkId=workId
                })>0;
            }
            return false;
        }
        /// <summary>
        /// 手机验证登录
        /// </summary>
        /// <param name="verificationCode"></param>
        /// <returns></returns>
        public VerificationCode GetMobilelogin(VerificationCode verificationCode)
        {
            string sql = "SELECT* FROM `verificationcode` WHERE `Phone` = @Phone AND `CodeNum` = @CodeNum";
            return DapperDBHelper<VerificationCode>.QueryFirstOrDefault(sql, verificationCode);
        }
        /// <summary>
        /// 通过手机号得到用户信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public UserInfo GetUserByPhone(string phone)
        {
            string sql = "SELECT * FROM userInfo WHERE phone=@phone";
            return DapperDBHelper<UserInfo>.QueryFirstOrDefault(sql, new { phone = phone });
        }
        /// <summary>
        /// 得到对应的用户信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public UserInfo GetUserInfo(int Id)
        {
            string sql = "select * from userInfo where UserId=@userId";
            return DapperDBHelper<UserInfo>.QueryFirstOrDefault(sql, new { userId = Id });
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <returns></returns>
        public int UpdateUserInfo(UserInfo userInfo) {
            string sql = "UPDATE userInfo SET userName=@userName , Email=@Email ,Phone=@Phone WHERE UserId=@UserId";
            return DapperDBHelper<UserInfo>.Execute(sql, userInfo);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldpassword"></param>
        /// <param name="newpassword"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int UpdatePassword(string oldpassword, string newpassword, int userId) {
            string sql = "UPDATE userInfo SET userPwd=@newpassword WHERE UserId=@userId AND userPwd=@oldpassword";
            return DapperDBHelper<UserInfo>.Execute(sql,new { oldpassword=oldpassword,userId=userId,newpassword=newpassword});
        }

        /// <summary>
        /// 通过QQ得到用户信息
        /// </summary>
        /// <param name="QQ"></param>
        /// <returns></returns>
        public UserInfo GetUserInfoByQQ(string QQ) {
            string sql = "SELECT * FROM userInfo WHERE qq=@qq";
            return DapperDBHelper<UserInfo>.QueryFirstOrDefault(sql,new { qq=QQ});
        }
    }
}

